using System;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameScreen
{
    public class GameScreen
    {
        protected ScreenManager manager;
        protected ContentManager content;
        public float TransTime = 0;
        public Transition Trans = Transition.None;
        protected float Alpha = 0;
        protected int Duration = 0;

        public bool LoadComplete
        {
            get { return loadComplete; }
            protected set { loadComplete = value; }
        }
        bool loadComplete = false;

        public bool TransComplete
        {
            get { return transComplete; }
            protected set { transComplete = value; }
        }
        bool transComplete = false;

        public GameScreen(ScreenManager man)
        {
            manager = man;
            content = new ContentManager(manager.Game.Services, "Content");
        }

        public void FadeOut()
        {
            Trans = Transition.FadeOut;
            transComplete = false;
            TransTime = 0;
            Duration = 1000;
            Alpha = 0;
        }

        public void FadeIn()
        {
            Trans = Transition.FadeIn;
            transComplete = false;
            TransTime = 1000;
            Duration = 1000;
            Alpha = 1;
        }

        public virtual void Load() { }

        public virtual void UnLoad()
        {
            content.Unload();
        }

        public virtual void Update(GameTime gametime)
        {
            switch (Trans)
            {
                case Transition.FadeIn:
                    TransTime -= gametime.ElapsedGameTime.Milliseconds;
                    if (TransTime <= 0)
                    {
                        transComplete = true;
                        this.Trans = Transition.None;
                    }
                    Alpha = TransTime / Duration;
                    break;

                case Transition.FadeOut:
                    TransTime += gametime.ElapsedGameTime.Milliseconds;
                    if (TransTime >= Duration)
                    {
                        transComplete = true;
                        this.Trans = Transition.None;
                    }
                    Alpha = TransTime / Duration;
                    break;
            }
        }

        public virtual void Draw(SpriteBatch spritebatch)
        {
            if (Trans != Transition.None)
            {
                manager.FadeBackBufferToBlack(Alpha);
            }
        }
    }
}
